import { store } from "~store"
import { setAutoSetCounterListStor, segtNewProInstall } from "~store/controller/autoCounps"

export const aoutCoup = (config?: any) => {
    // console.log('aoutCoup0', config)
    window.postMessage({
        type_urs: 'wtbg',
        method: 'add_coupon_config',
        configs: config
    })

}

function lodinSters(e) {
    if (e.data.type_urs === 'wtbg' && e.data.method === 'saveLocstros') {
        // console.log('前端收到并保存')
        store.dispatch(setAutoSetCounterListStor(e.data.data))
        // console.log('autoCoupStart', e.data.data)
        // localStorage.setItem('autoSetCoupon', JSON.stringify(e.data.data))
        // console.log('通知到页面修改数据')
        // console.log('autoCoupStart', e.data)
    }
}

function getconter(e) {
    if (e.data.type_urs === 'wtbg' && e.data.method === 'getNewPrdHoter') {
        // console.log('前端收到并保存是否哦安装12345')
        store.dispatch(segtNewProInstall(true))
        // console.log('autoCoupStart', e.data.data)
        // localStorage.setItem('autoSetCoupon', JSON.stringify(e.data.data))
        // console.log('通知到页面修改数据')
        // console.log('autoCoupStart', e.data)
    }
}


export const autoCoupStart = (config?: any) => {
    window.postMessage({
        type_urs: 'wtbg',
        method: 'run_auto_set_coupon',
        configs: config || []
    })
    window.removeEventListener('message', lodinSters)
    window.addEventListener('message', lodinSters)
}

export const getBgCoupOuterTuis = (config?: any) => {
    window.postMessage({
        type_urs: 'wtbg',
        method: 'get_auto_set_coupon',
        configs: config || []
    })
    window.removeEventListener('message', lodinSters)
    window.addEventListener('message', lodinSters)
}

export const loginShopGetShopInfos = (config?: any) => {
    window.postMessage({
        type_urs: 'wtbg',
        method: 'save_user_login_shop_data',
        configs: config || []
    })
    window.removeEventListener('message', lodinSters)
    window.addEventListener('message', lodinSters)
    // window.removeEventListener('message', lodinSters)
    // window.addEventListener('message', lodinSters)
}

export const addNewConter = (config?: any) => {
    window.postMessage({
        type_urs: 'wtbg',
        method: 'get_add_new_prd',
        configs: config || []
    })
    window.removeEventListener('message', getconter)
    window.addEventListener('message', getconter)
    // window.removeEventListener('message', lodinSters)
    // window.addEventListener('message', lodinSters)
}


export const saveUserJwt = (jwt?: any) => {
    window.postMessage({
        type_urs: 'wtbg',
        method: 'save_user_jwt',
        // configs: config || [],
        jwt: jwt
    })
    // window.removeEventListener('message', lodinSters)
    // window.addEventListener('message', lodinSters)
    // window.removeEventListener('message', lodinSters)
    // window.addEventListener('message', lodinSters)
}

export const saveRunDentTime = (time) => {
    window.postMessage({
        type_urs: 'wtbg',
        method: 'save_run_dent_time',
        time: time
    })
}